﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Drawing.Imaging;
using DevExpress.XtraGrid.Views.Card.ViewInfo;

namespace TS_XY_Train_Repaire.ToolSystem
{
    public partial class Tool_System_ToolInput_Form : DevExpress.XtraEditors.XtraForm
    {
        public Tool_System_ToolInput_Form()
        {
            InitializeComponent();
        }
  
        private void Tool_System_ToolInput_Form_Load(object sender, EventArgs e)
        {
            _LoadGrid();
        }

        private void bbi_Add_Tool_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            //
            Tool_System_ToolEdit_Form oForm = new Tool_System_ToolEdit_Form();
            if (oForm.ShowDialog() == DialogResult.OK)
            {
                _LoadGrid();
            }
        }

        private void bbi_Modify_Tool_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int index = cv_view.GetFocusedDataSourceRowIndex();
            if (index < 0)
            {
                return;
            }

            DataRow vCurrenRow = cv_view.GetFocusedDataRow();
            Tool_System_ToolEdit_Form oForm = new Tool_System_ToolEdit_Form();
            oForm.m_oTool.IID = (int)vCurrenRow["f_id"];
            oForm.m_oTool.ITypeID = (int)vCurrenRow["f_typeid"];
            oForm.m_oTool.SRfID = vCurrenRow["f_rfid"].ToString();
            oForm.m_oTool.IPosID = (int)vCurrenRow["f_position"];
            oForm.m_oTool.IParentID = (int)vCurrenRow["f_parentid"];
            oForm.m_oTool.IStatusType = (int)vCurrenRow["f_statetype"];
            oForm.m_oTool.SFactoryID = vCurrenRow["f_fcid"].ToString();
            //Type oType =  vCurrenRow[7].GetType();
            oForm.m_oTool.FPrice = (double)vCurrenRow["f_fcprice"];
            oForm.m_oTool.OFcDate = DateTime.Parse(vCurrenRow["f_fcdate"].ToString());
            oForm.m_oTool.SFcCode = vCurrenRow["f_fccode"].ToString();

            if (oForm.ShowDialog() == DialogResult.OK)
            {
                _LoadGrid();
            }
        }

        private void bbi_Del_Tool_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int index = cv_view.GetFocusedDataSourceRowIndex();
            if (index < 0)
            {
                return;
            }

            DataRow vCurrenRow = cv_view.GetFocusedDataRow();
            int iToolID = (int)vCurrenRow[0];

            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
            String strMsg = "确定要删除序号为" + iToolID.ToString() + "的工具吗？";
            DialogResult dr = MessageBox.Show(strMsg, "删除工具", messButton);
            if (dr == DialogResult.OK)//如果点击“确定”按钮
            {
                if (ToolDataMgr.GetInstance().DeleteTool(iToolID))
                {
                    _LoadGrid();
                }
            }
        }

        // 加载工具列表
        private void _LoadGrid()
        {
            try
            {
                DataTable dt = ToolDataMgr.GetInstance().GetToolListDataTable();
                dt.Columns.Add(new DataColumn("f_photo", typeof(byte[])));

                foreach (DataRow dataRow in dt.Rows)
                {
                    Image image = Utility.ChangeStringToImage(dataRow["f_picture"].ToString());
                    if (image != null)
                    {
                        MemoryStream ms = new MemoryStream();
                        image.Save(ms, ImageFormat.Png);
                        var result = ms.ToArray();

                        dataRow["f_photo"] = ms.ToArray();
                    }
                    //dataRow["f_photo"] = image;
                }
                gc_Main.DataSource = dt;
            }
            catch
            {
                gc_Main.DataSource = null;
                gc_Main.Refresh();
            }
        }

        private void cv_view_CustomDrawCardCaption(object sender, DevExpress.XtraGrid.Views.Card.CardCaptionCustomDrawEventArgs e)
        {
            DataRow dataRow = cv_view.GetDataRow(e.RowHandle) as DataRow;
            if (dataRow != null)
            {
                e.CardCaption = "工具序号:" + dataRow["f_id"].ToString();
            }

            ToolStatus oStatus = (ToolStatus)int.Parse(dataRow["f_statetype"].ToString());
            //if (oStatus == ToolStatus.Normal)
            //    e.bac
        }

        private void cv_view_CustomDrawCardFieldValue(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            //DataRow dataRow = cv_view.GetDataRow(e.RowHandle) as DataRow;
            //if (dataRow != null)
            //{
            //    Image image = Utility.ChangeStringToImage(dataRow["f_picture"].ToString());
            //    e.Column["f_picture"] = image;
            //}
        }

        private void Tool_System_ToolInput_Form_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }

        private void cv_view_DoubleClick(object sender, EventArgs e)
        {
            
        }

        private void cv_view_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Clicks == 2 && e.Button == MouseButtons.Left)
            {
                CardHitInfo hInfo = cv_view.CalcHitInfo(new Point(e.X, e.Y));
                if (hInfo.InCard)
                    bbi_Modify_Tool.PerformClick();
            }
        }
    }
}